import base64
import string, os, sys, httplib, urllib2
import pdb

server_addr = "http://109.233.224.68:2237"
service_action = "/magicsys"
username = "magicsys"
password = "magicsys"

body = """
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<ws:PutMessage>
<ObjectID>89700415</ObjectID>
<MessageText>TEST</MessageText>
<Time>2012-11-28T16:47:00Z</Time>
</ws:PutMessage>
</soapenv:Body>
</soapenv:Envelope>"""

body = """
<?xml version="1.0" encoding="windows-1251"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<ws:PutCoord>
<ObjectID>89700415</ObjectID>
<Coord time="2012-11-28T16:30:01Z" lon="37.754689" lat="55.6586458" speed="20.1" dir="301" valid="1"/>
</ws:PutCoord>
</soapenv:Body>
</soapenv:Envelope>"""


'''
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, server_addr + service_action, username, password)
auth_handler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
response = urllib2.urlopen(server_addr + service_action, '')
'''

req = urllib2.Request(server_addr + service_action)
auth = 'Basic ' + base64.urlsafe_b64encode('%s:%s' % (username, password))
req.add_header('Authorization', auth)
req.add_header('SOAPAction', '""')
req.add_header('Content-Length', str(len(body)))
req.add_data(body)
conn = urllib2.urlopen(req)

